package t3dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import t3jdbc.JDBCUtil;
import t3bean.MoneyRecordBean;

public class MoneyRecordDao extends Dao {
	private SetSql ss = new SetSql();

	public int insert(MoneyRecordBean mrb) {
		String sql = "insert into moneyrecord(car_id,space_id,money,parkingtime,gettime) values(?,?,?,?,?)";
		List<Object> list = new ArrayList<>();
		list.add(mrb.getCarId());
		list.add(mrb.getSpaceId());
		list.add(mrb.getMoney());
		list.add(mrb.getParkingtime());
		list.add(mrb.getGettime());

		int back = 0;
		try {
			back = dml(sql, list);
			return back;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public List<MoneyRecordBean> select(MoneyRecordBean mrb) throws SQLException {
		String sql;
		List<MoneyRecordBean> backList = new ArrayList<>();
		List<Object> list = new ArrayList<>();
		ResultSet rs = null;
		if (mrb != null) {
			sql = "select * from moneyrecord where ";
			sql = ss.setSql(mrb, sql, list, 2);
//			System.out.println(sql);
		} else {
			sql = "select * from moneyrecord";
		}

		try {
			rs = dql(sql, list);
			while (rs.next()) {
				MoneyRecordBean mrb1 = new MoneyRecordBean();
				mrb1.setCarId(rs.getString("car_id"));
				mrb1.setId(rs.getInt("id"));
				mrb1.setMoney(rs.getInt("money"));
				mrb1.setSpaceId(rs.getInt("space_id"));
				Timestamp time1 = rs.getTimestamp("parkingtime");
				mrb1.setParkingtime(time1.toLocalDateTime());
				Timestamp time2 = rs.getTimestamp("gettime");
				mrb1.setGettime(time2.toLocalDateTime());
				backList.add(mrb1);
			}
			return backList;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(super.conn, super.ps, super.rs);
			super.conn = null;
			super.ps = null;
			super.rs = null;
			rs.close();
			rs = null;
		}
		return null;
	}
}
